import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'


Vue.use(VueRouter)

const routes = [
  {path: '/',  redirect: '/login'},
  {path: '/login', component: login},
  {path: '/register', component: () => import('@/views/register')},
  {path: '/index', redirect: '/index/employees', component: () => import('@/views/index'), children: [
    {path: 'employees', component: () => import('@/components/menus/Employees')},
    {path: 'roleapply', component: () => import('@/components/menus/Roleapply')},
    {path: 'personal', component: () => import('@/components/menus/Personal')},
    {path: 'audit', component: () => import('@/components/menus/Audit')},
    {path: 'detail/:id', component: () => import('@/components/menus/Detail'), props: true},
    {path: 'notice', component: () => import('@/components/menus/Notice')},
  ]},
  {path: '*', component: () => import('@/views/404')}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if(to.path === '/login' || to.path === '/register'){
    next()
  }else {
    if(!token) return next('/login')
    next()
  }
})

export default router
